<!-- src/views/EssayGrader.vue -->
<template>
  <div class="essay-grader-container">
    <h1>作文智能批改系统</h1>
    
    <!-- 作文输入区域 -->
    <div class="essay-input">
      <h3>请输入或粘贴作文内容：</h3>
      <textarea 
        v-model="essayText" 
        placeholder="在这里输入作文内容..." 
        rows="15"
      ></textarea>
      <button 
        @click="submitEssay" 
        :disabled="isSubmitting"
        class="submit-btn"
      >
        {{ isSubmitting ? '批改中...' : '提交批改' }}
      </button>
    </div>

    <!-- 批改结果展示 -->
    <div v-if="result" class="result-container">
      <h3>批改结果：</h3>
      <div class="result-content" v-html="formattedResult"></div>
      
      <button 
        v-if="result" 
        @click="saveResult"
        class="save-btn"
      >
        保存结果
      </button>
    </div>

    <!-- 错误提示 -->
    <div v-if="error" class="error-message">
      {{ error }}
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      essayText: '',
      result: '',
      error: '',
      isSubmitting: false
    }
  },
  computed: {
    formattedResult() {
      // 将批改结果中的换行符转换为HTML换行标签
      return this.result.replace(/\n/g, '<br>');
    }
  },
  methods: {
    async submitEssay() {
      // 验证输入
      if (!this.essayText.trim()) {
        this.error = '请输入作文内容';
        return;
      }

      this.error = '';
      this.isSubmitting = true;

      try {
        const response = await axios.post('/api/rate-essay', {
          text: this.essayText
        });
        
        this.result = response.data.result;
      } catch (err) {
        console.error('批改失败:', err);
        this.error = err.response?.data?.error || '批改服务暂时不可用，请稍后再试';
      } finally {
        this.isSubmitting = false;
      }
    },
    saveResult() {
      // 这里可以添加保存到本地或后端的逻辑
      alert('批改结果已保存（示例功能）');
      // 实际实现可能需要调用另一个API端点
    }
  }
}
</script>

<style scoped>
.essay-grader-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

h1 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 30px;
}

.essay-input {
  margin-bottom: 30px;
}

textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  margin-bottom: 15px;
  resize: vertical;
}

button {
  padding: 10px 20px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #3aa876;
}

button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.submit-btn {
  background-color: #3498db;
}

.save-btn {
  background-color: #9b59b6;
  margin-top: 15px;
}

.result-container {
  background-color: #f9f9f9;
  padding: 20px;
  border-radius: 4px;
  margin-top: 20px;
}

.result-content {
  white-space: pre-wrap;
  line-height: 1.6;
}

.error-message {
  color: #e74c3c;
  padding: 10px;
  background-color: #fadbd8;
  border-radius: 4px;
  margin-top: 20px;
}
</style>